package com.shms.dao.impl;

import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.shms.bean.GroupOfStudent;
import com.shms.dao.ShStudentDao;
import com.shms.entity.ShStudent;
import com.shms.until.PageUntil;

/**
 * 文件名：ShStudentDaoImpl.java
 * 编写者：
 * 创建时间：2013-1-3
 * 简要描述：
 *
 */
public class ShStudentDaoImpl extends HibernateDaoSupport implements ShStudentDao {
	
	@SuppressWarnings("finally")
	public GroupOfStudent queryStudent(GroupOfStudent groupOfStudent) {
		GroupOfStudent gOfStudentRtn = new GroupOfStudent();
		try {
			String sql = "SELECT COUNT(*) FROM SH_STUDENT S";
			
			SQLQuery sqlQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(sql);
			//获取总条数
			int allRow = Integer.parseInt(String.valueOf(sqlQuery.uniqueResult()));
			//获取总页数
			int totalPage = PageUntil.countTotalPage(groupOfStudent.getPageSize(), allRow);
			//获取要查询显示的数据的页数
			int currentPage = PageUntil.countCurrentPage(groupOfStudent.getCurrentPage(), totalPage);
			//获取要查询显示的数据的第一条的数字
			int currentRow = PageUntil.countCurrentRow(groupOfStudent.getPageSize(), currentPage);
			
			StringBuffer strSql = new StringBuffer();
			strSql.append("SELECT S.SNO AS sno, S.SNAME AS sname, S.SSEX AS ssex, B.BNAME AS park1, H.HNO AS park2, C.CNAME AS park3");
			strSql.append(" FROM SH_STUDENT S, SH_CLASS C, SH_HOME H, SH_BUILD B");
			strSql.append(" WHERE S.CID = C.CID AND S.HID = H.HID AND S.BID = B.BID");
			strSql.append(" ORDER BY S.SNO");
			sqlQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(strSql.toString());
			sqlQuery.addScalar("sno", Hibernate.STRING).addScalar("sname", Hibernate.STRING).addScalar("ssex", Hibernate.STRING).addScalar("park1", Hibernate.STRING).addScalar("park2", Hibernate.STRING).addScalar("park3", Hibernate.STRING);
			//查询结果转换为T对象
			sqlQuery.setResultTransformer(Transformers.aliasToBean(ShStudent.class));
			//设置查询的第一条数据位置
			sqlQuery.setFirstResult(currentRow);
			//设置查询最大条数
			sqlQuery.setMaxResults(groupOfStudent.getPageSize());
			@SuppressWarnings("unchecked")
			List<ShStudent> studentList = sqlQuery.list();
			gOfStudentRtn = new GroupOfStudent(currentPage, totalPage, allRow, studentList); 
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			return gOfStudentRtn;
		}
	}
	
	@SuppressWarnings("finally")
	public boolean batchDelete(Integer[] ids) {
		boolean rtn = true;
		try {
			String sql = "DELETE FROM SH_STUDENT S WHERE S.SNO IN (:ids)";
			SQLQuery sqlQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(sql);
			sqlQuery.setParameterList("ids", ids);
			sqlQuery.executeUpdate();
		} catch (Exception ex) {
			ex.printStackTrace();
			rtn = false;
		} finally {
			return rtn;
		}
	}
	
	@SuppressWarnings("finally")
	public boolean saveStudent(ShStudent shStudent) {
		boolean rtn = true;
		try {
			this.getSessionFactory().getCurrentSession().save(shStudent);
		} catch (Exception ex) {
			ex.printStackTrace();
			rtn = false;
		} finally {
			return rtn;
		}
	}
	
	@SuppressWarnings("finally")
	public boolean updateStudent(ShStudent shStudent) {
		boolean rtn = true;
		try {
			this.getSessionFactory().getCurrentSession().update(shStudent);
		} catch (Exception ex) {
			ex.printStackTrace();
			rtn = false;
		} finally {
			return rtn;
		}
	}
	
	@SuppressWarnings("finally")
	public ShStudent queryByNo(ShStudent shStudent) {
		ShStudent shStudentRtn = new ShStudent();
		try {
			String sql = "SELECT * FROM SH_STUDENT WHERE SNO = :sno";
			SQLQuery sqlQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(sql);
			sqlQuery.setProperties(shStudent);
			@SuppressWarnings("unchecked")
			List<ShStudent> listStudent = sqlQuery.addEntity(ShStudent.class).list();
			if (listStudent.size() == 1) {
				shStudentRtn = listStudent.get(0);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			return shStudentRtn;
		}
	}
	
	@SuppressWarnings("finally")
	public boolean checkStudent(ShStudent shStudent){
		boolean check = false;
		try {
			String sql = "SELECT * FROM SH_STUDENT WHERE SNO = :sno";
			SQLQuery sqlQuery = this.getSessionFactory().getCurrentSession().createSQLQuery(sql);
			sqlQuery.setProperties(shStudent);
			@SuppressWarnings("unchecked")
			List<ShStudent> listStudent = sqlQuery.addEntity(ShStudent.class).list();
			if (listStudent.size() == 1) {
				check = true;
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			check = false;
		} finally {
			return check;
		}
	}
}
